package ink.teamwork.stock.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import ink.teamwork.stock.dao.StrategyDAO;
import ink.teamwork.stock.entity.Strategy;
import ink.teamwork.stock.service.StrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class StrategyServiceImpl implements StrategyService {

    @Autowired
    private StrategyDAO strategyDAO;

    public boolean save(Strategy strategy) {
        return strategyDAO.save(strategy);
    }

    public boolean update(Strategy strategy) {
        try {
            strategyDAO.save(strategy);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public SearchResult<Strategy> queryPageByUserId(int start, int size, String userId) {
        Search search = new Search();
        search.addFilterEqual("userId", userId);
        search.setFirstResult(start);
        search.setMaxResults(size);
        return strategyDAO.searchAndCount(search);
    }

    public List<Strategy> queryListByUserId(String userId) {
        Search search = new Search();
        search.addFilterEqual("userId", userId);
        return strategyDAO.search(search);
    }
}
